package com.smalljj.comm;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Util {
	
	//base64编码
	//String 变量=Base64.encodeToString(字符串.getBytes(),Base64.DEFAULT);
	
	/**
	 * 获取网页内容
	 * @param url
	 * @return
	 */
	public static String getWebSource(String url) {
		String sCurrentLine = null;
		StringBuffer sTotalString = new StringBuffer("");
		BufferedReader buffer = null;    
		try {
	        // 创建一个URL对象    
			URL urlObj = new URL(url);    
			HttpURLConnection urlConn = (HttpURLConnection) urlObj.openConnection();
			buffer = new BufferedReader(new InputStreamReader(     urlConn.getInputStream()));    
			while ((sCurrentLine = buffer.readLine()) != null) {
				sTotalString.append( sCurrentLine) ;
			}
		} catch (Exception e) {
			return "";
		}finally {    
	        try {    
	            buffer.close();    
	        } catch (Exception e) {
	        	e.printStackTrace();
	        }    
	    }    
		return sTotalString.toString();
	}

	/**
	 * 得到当前时间getCurrentFormatStr
	 * 
	 * @param strformat
	 * @return String
	 * 
	 */
	public static String getCurrentFormatStr(String strformat) {
		SimpleDateFormat formatter = new SimpleDateFormat(strformat);
		String sr = formatter.format(new Date());
		return sr;
	}


    /**
     * 检查新版本
     */
	public static String checkNewVersion() {
		String newVersionString = Util.getWebSource(Constants.VERSIONFILE);
		if( newVersionString.equals("")==false &&
				newVersionString.equalsIgnoreCase(Constants.CURRENTVERSION)==false ){
			return newVersionString;
		}
		return "";
	}
}